<script setup lang="ts">
import AddWrite from './components/addWrite.vue'
import { ContentDetailWrap } from '@/components/ContentDetailWrap'
import { ref, unref } from 'vue'
import { ElButton } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { useRouter, useRoute } from 'vue-router'
import { sendEdit, lookEdit } from '@/api/common'
import { TableData } from '@/api/table/types'
import { useEmitt } from '@/hooks/web/useEmitt'

const { emitter } = useEmitt()

const { push } = useRouter()

const { query } = useRoute()

const { t } = useI18n()

const currentRow = ref<Nullable<TableData>>(null)

const getTableDet = async () => {
  const res = await lookEdit(query.id as string)
  if (res) {
    console.log('---获取row内容---')
    currentRow.value = res.data
  }
}

getTableDet()

const addWrite = ref<ComponentRef<typeof AddWrite>>()

const loading = ref(false)

// 这个save是点击保存执行的方法  所以 我们需要在这里拿到填写的数据  然后去调用接口  --  明白吗？
const save = async () => {
  const write = unref(addWrite)
  // 1、获取填写的数据
  let formData = write?.getFormData() // 这个就是addWrite 里头写的方法  是用来获取填写的数据的
  console.log('---输出填写的表单数据---', formData)
  let imgs: any = []
  formData.images.forEach((item) => {
    imgs.push(item.url)
  })
  // 2、看看接口需要什么数据
  let apiData = {
    transport_id: formData.modeOfTransport, //运输方式
    transport_text: formData.modeOfTransportText, // 运输方式中文,
    client_code: formData.customerManagement, // 客户号
    goods_id: formData.typeOfGoods[0], //货物种类
    goods_text: formData.typeOfGoodsText, // 获取种类中文
    fahuo: formData.shipper, //出库人
    shouhuo: formData.consignee, //收货人
    weight: formData.totalWeight, //重量合计
    quantity: formData.number, //数量
    volume: formData.totalVolume, //体积合计
    density: formData.density, //密度
    shouhuo_mobile: formData.consigneePhone, //收货人电话
    units: formData.totalNumberOfpices, //件数合计
    placestart_id: formData.origin, //始发地
    placestart_text: formData.originText, //始发地的中文
    placeend_id: formData.address, //目的地
    placeend_text: formData.addressText, //目的地中文
    pack_id: formData.packagingType[0], //包装方式
    pack_text: formData.packagingTypeText, //包装方式zw
    submarket_id: formData.distributionMarket, //分货市场
    submarket_text: formData.distributionMarketText, //分货市场zw
    salesman: formData.salespersonName, //业务员姓名
    base_price: formData.businessFloorPrice, //业务底价
    base_freight: formData.businessFloorreight, //业务底价运费
    cny_value: formData.RMBPrice, //人民币货值
    usd_value: formData.dollarPrice, //美元货值
    usd_cny: formData.exchangeRate, //汇率:美元-人民币
    insurance: formData.bvjs, //保率方式:1=百货,2=服装,99=手动
    insurance_rate: formData.guaranteeRate, //保率
    kilo_value: formData.valuePreservationPerKilogram, //每公斤货值
    insurance_fee: formData.businessFloorPrice, //应收保险费
    pack_type: formData.packagingCalculation, //包装计算方式:1=按件,2=按重
    pack_fee: formData.totalNumberOfpices, //包装费用
    package: formData.packingCharge, //打包费用
    transport_type: formData.freightCalculationMethod, //运费计算方式:1=按件,2=按重量,99=手动
    transport_price: formData.fieghtPrice, //运费价格
    transport_fee: formData.freight, //运费
    short_fee_cny: formData.shortdistancefee, //短途费
    short_fee_usd: formData.shortdistancefeeUSD, //短途费美金
    agency_fund: formData.invoicing, //代收款
    other_fund: formData.otherReceivables, //其他应收款
    append_fund: formData.surcharge, //附加费
    paytype: formData.paymentMethod, //付款类型未看到具体的暂定，1=，2=3=，4=
    total_fee: formData.totalReceivableExpenses, //应收总费用
    explain: formData.illustrate, //说明
    remark: formData.notes, //备注
    imitate: formData.whetherToImitateTheBrand, //是否仿牌
    state: formData.state, //单据状态
    audit: formData.audit, //审核人
    enact: formData.enact, //制单人
    enacttime: formData.enacttime, //制单时间
    brokerage_name: formData.brokerage_name, //返佣姓名
    brokerage_rate: formData.brokerage_rate, //返佣比例
    brokerage_money: formData.brokerage_money, //返佣金额
    brokerage_status: formData.brokerage_status, //返佣状态
    brokerage_remark: formData.brokerage_remark, //返佣备注
    foreign_charge: formData.foreignExpenses, //国外费用
    remark2: formData.productNameRemarks, //品名备注
    goods_fee: formData.mustPrice, //应收货款
    transport_expires: formData.transport_expires, //运输期限
    goods: JSON.stringify(write?.getTabelData()), //货物
    images: imgs
  }
  console.log('---apiData-接口需要的数据---', apiData)
  // 3、调用接口
  let res = await sendEdit({ ...apiData }, query.id as string)
  console.log('---新增接口调用---', res)
  if (res) {
    emitter.emit('getList', 'edit')
    push('/logistics/arrivalNoteEnd')
  }
}
</script>
<template>
  <ContentDetailWrap :title="t('exampleDemo.edit')" @back="push('/logistics/arrivalNoteEnd')">
    <!-- <Write ref="writeRef" /> -->
    <AddWrite ref="addWrite" :current-row="currentRow" />

    <template #right>
      <ElButton type="primary" :loading="loading" @click="save">
        {{ t('exampleDemo.save') }}
      </ElButton>
    </template>
  </ContentDetailWrap>
</template>
